package com.bringway.p2p.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.bringway.framework.dao.P2PJdbcDao;
import com.bringway.p2p.dao.ImageDao;

import net.sf.json.JSONObject;

@Repository
public class ImageDaoImpl extends P2PJdbcDao implements ImageDao {
	
	private Logger logger = Logger.getLogger(ComNewsDaoImpl.class);
	
	@Override
	public int insertImage(Map<String, Object> paramMap) {
		String sql = "insert into t_zhfw_index_banner(ID,REF_ATT,OPER_USERNAME,CREATE_DATE,ORDER_NUM,IMG_TITLE,IMG_URL,IMG_STATE) values(UUID(),:attid,:userid,now(),:paixu,:title,:ul,'00')";
		logger.info("insertImage sql is " + sql);
		logger.info("insertImage paramMap is " + paramMap);
		return this.getNamedParameterJdbcTemplate().update(sql, paramMap);
	}

	@Override
	public List<Map<String, Object>> queryImagesList(JSONObject params) {
		int start = params.optInt("start");
		int limit = params.optInt("limit");
		String startDate = params.optString("startDate");
		String endDate = params.optString("endDate");
		String title = params.optString("title");
		String imgState = params.optString("imgState");
		System.out.println(imgState);
		Map<String, Object> paramMap = new HashMap<>();
		paramMap.put("start", start);
		paramMap.put("limit", limit);
		StringBuffer sql = new StringBuffer();
		sql.append("select ID,REF_ATT,OPER_USERNAME,date_format(FB_DATE,'%Y-%m-%d') FB_DATE,date_format(CREATE_DATE,'%Y-%m-%d %H:%i:%s') CREATE_DATE,IMG_STATE,IMG_TITLE,IMG_URL,ORDER_NUM from t_zhfw_index_banner where 1=1");
		if (StringUtils.isNotEmpty(startDate)) {
			sql.append(" and date_format(FB_DATE,'%Y-%m-%d')>=:startDate");
			paramMap.put("startDate", startDate);
		}
		if (StringUtils.isNotEmpty(endDate)) {
			sql.append(" and date_format(FB_DATE,'%Y-%m-%d')<=:endDate");
			paramMap.put("endDate", endDate);
		}
		if (StringUtils.isNotEmpty(title)) {
			sql.append(" and IMG_TITLE like :title");
			paramMap.put("title", "%" + title + "%");
		}
		if(StringUtils.isNotEmpty(imgState)){
			sql.append(" and IMG_STATE=:imgState");
			paramMap.put("imgState", imgState);
		}
		sql.append(" order by CREATE_DATE desc limit :start,:limit");
		logger.info("queryImagesList sql is " + sql.toString());
		logger.info("queryImagesList paramMap is " + paramMap);
		return this.getNamedParameterJdbcTemplate().queryForList(sql.toString(), paramMap);
	}

	@Override
	public int getImagesCount(JSONObject params) {
		String startDate = params.optString("startDate");
		String endDate = params.optString("endDate");
		String title = params.optString("title");
		String imgState = params.optString("imgState");
		Map<String, Object> paramMap = new HashMap<>();
		StringBuffer sql = new StringBuffer();
		sql.append("select count(*) from t_zhfw_index_banner  where 1=1");
		if (StringUtils.isNotEmpty(startDate)) {
			sql.append(" and date_format(CREATE_DATE,'%Y-%m-%d')>=:startDate");
			paramMap.put("startDate", startDate);
		}
		if (StringUtils.isNotEmpty(endDate)) {
			sql.append(" and date_format(CREATE_DATE,'%Y-%m-%d')<=:endDate");
			paramMap.put("endDate", endDate);
		}
		if (StringUtils.isNotEmpty(title)) {
			sql.append(" and IMG_TITLE like :title");
			paramMap.put("title", "%" + title + "%");
		}
		if (StringUtils.isNotEmpty(imgState)) {
			sql.append(" and IMG_STATE=:imgState");
			paramMap.put("imgState", imgState);
		}
		logger.info("getCount sql is " + sql.toString());
		logger.info("getCount paramMap is " + paramMap);
		return this.getNamedParameterJdbcTemplate().queryForInt(sql.toString(), paramMap);
	}

	@Override
	public int updateImages(Map<String, Object> paramMap) {
		String sql = "update t_zhfw_index_banner set REF_ATT=:attid,OPER_USERNAME=:userid,ORDER_NUM=:paixu,IMG_TITLE=:title,IMG_URL=:ul,FB_DATE=NULL,IMG_STATE='00' where ID=:id";
		logger.info("updateImages sql is" + sql.toString());
		logger.info("updateImages paramMap is " + paramMap);
		return this.getNamedParameterJdbcTemplate().update(sql.toString(), paramMap);
	}

	@Override
	public int publicImages(Map<String, Object> paramMap) {
		String sql = "update t_zhfw_index_banner set IMG_STATE=:state,FB_DATE=now() where ID=:id";
		logger.info("publicImages sql is" + sql.toString());
		logger.info("publicImages paramMap is" + paramMap);
		return this.getNamedParameterJdbcTemplate().update(sql.toString(), paramMap);
	}

	@Override
	public int HuiShouImage(Map<String, Object> paramMap) {
		String sql = "update t_zhfw_index_banner set IMG_STATE=:state,FB_DATE=NULL where ID=:id";
		logger.info("HuiShouImage sql is" + sql.toString());
		logger.info("publicImages paramMap is" + paramMap);
		return this.getNamedParameterJdbcTemplate().update(sql.toString(), paramMap);
	}

	@Override
	public int updateAllImages(Map<String, Object> paramMap) {
		String sql = "update t_zhfw_index_banner set IMG_STATE='02',FB_DATE=NULL where IMG_STATE='01' and ORDER_NUM=:orderNum";
		logger.info("updateAllImages sql is" + sql.toString());
		logger.info("updateAllImages paramMap is" + paramMap);
		return this.getNamedParameterJdbcTemplate().update(sql.toString(), paramMap);
	}

}
